﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

/// <summary>
/// UI界面管理
/// </summary>
public class CanvasManager : MonoBehaviour
{
    public static CanvasManager Instance;

    [Header("UI界面")]
    public GameObject gameMenu;
    public GameObject selectMenu;
    public GameObject passengerMenu;
    public GameObject completeMenu;
    public Image loadMask;
    public DOTweenAnimation kefuPopup;
    [Header("选择效果")]
    public GameObject choiceEff;
    public Text choiceResult;
    public Color color_Green;
    public Color color_Green_T;
    public Color color_Red;
    public Color color_Red_T;
    [Header("结算星星")]
    public List<GameObject> stars;
    public Text rewardText;
    public GameObject doubleBtn;
    private int currentReward = 0;
    private int doubleReward = 0;

    // 下一界面
    private GameObject currentUI;
    private GameObject nextUI;
    [Header("模型动画")]
    public Animator modelAnim;
    private bool changeGender;
    [Header("相机管理器")]
    public CameraManager cameraManager;

    [Header("日期")]
    public Text dateText;
    public Text moneyText;

    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {
        PlayerPrefs.DeleteAll();
        UIInit();
        cameraManager = CameraManager.Instance;
    }

    // 界面初始化
    private void UIInit()
    {
        currentUI = gameMenu;
        gameMenu.SetActive(true);
        selectMenu.SetActive(false);
        passengerMenu.SetActive(false);
        completeMenu.SetActive(false);
        nextUI = null;

        UpdateDate();
        AudioManager.Instance.BGMController(true);
    }

    private void UpdateDate()
    {
        dateText.text = PlayerPrefs.GetInt("Date", 1).ToString();
        moneyText.text = PlayerPrefs.GetInt("Money", 100).ToString();
    }

    public void HideKefuPopup()
    {
        Invoke("DelayHidePopup", 1f);
    }

    private void DelayHidePopup()
    {
        kefuPopup.DOPlayBackwards();
    }

    // 转换性别
    public void OnGenderBtnClick()
    {
        Debug.Log("切换角色性别");
        AudioManager.Instance.PlayButtonSound();
        changeGender = true;
        if (PlayerPrefs.GetInt("Gender", 0) == 0)
            PlayerPrefs.SetInt("Gender", 1);
        else
            PlayerPrefs.SetInt("Gender", 0);
        LoadMaskAnim(0f);
    }

    // 开始
    public void OnPlayBtnClick()
    {
        AudioManager.Instance.PlayButtonSound();
        modelAnim.SetBool("Walk", true);
        modelAnim.transform.DOLocalMove(new Vector3(0, 0, 2f), 1.25f).SetEase(Ease.Linear);
        nextUI = selectMenu;
        LoadMaskAnim(0.25f);
    }

    // 返回主界面
    public void OnBlackBtnClikc()
    {
        AudioManager.Instance.PlayButtonSound();
        modelAnim.SetBool("Walk", false);
        modelAnim.SetTrigger("Idle");
        modelAnim.transform.DOLocalMove(new Vector3(0, 0, 0f), 0.1f).SetEase(Ease.Linear);
        nextUI = gameMenu;
        if (PassengerManager.Instance.currentPassenger.Count > 0)
        {
            PassengerManager.Instance.ClearAllPassenger();
            LoadMaskAnim(0f);
        }
        else
            LoadMaskAnim(0.25f);
    }

    // 进入游戏
    public void OnSelectBtnClick()
    {
        AudioManager.Instance.PlayButtonSound();
        nextUI = passengerMenu;
        LoadMaskAnim(0.25f);
    }

    // 跳过检查
    public void OnSkipBtnClick()
    {
        AudioManager.Instance.PlayButtonSound();

        Debug.Log("跳过检查");
        PassengerManager.Instance.scanRightNum = 3;
        PassengerManager.Instance.ClearAllPassenger();
        ShowCompleteMenu();
    }

    // 结算界面
    public void ShowCompleteMenu()
    {
        nextUI = completeMenu;
        LoadMaskAnim(0f);
        ShowStarsNum();
    }

    // 显示玩家选择效果
    public void ShowChoiceEff(bool right, string result = null)
    {
        choiceEff.SetActive(true);
        choiceResult.text = "";
        if (right)
        {
            choiceEff.GetComponent<Image>().color = color_Green;
            choiceEff.GetComponent<Image>().DOFade(1, 0.5f).OnComplete(() =>
             {
                 if (result != null)
                 {
                     choiceResult.color = color_Red_T;
                     choiceResult.text = result;
                 }
                 Invoke("HideChoiceEff", 1f);
             });
        }
        else
        {
            choiceEff.GetComponent<Image>().color = color_Red;
            choiceEff.GetComponent<Image>().DOFade(1, 0.5f).OnComplete(() =>
            {
                if (result != null)
                {
                    choiceResult.color = color_Green_T;
                    choiceResult.text = result;
                }
                Invoke("HideChoiceEff", 0.85f);
            });
        }
    }

    private void HideChoiceEff()
    {
        choiceResult.text = "";
        choiceEff.GetComponent<Image>().DOFade(0, 0.5f).OnComplete(() =>
        {
            choiceEff.SetActive(false);
        });
    }

    // 显示星星数量
    private void ShowStarsNum()
    {
        if (PassengerManager.Instance.scanRightNum == 0)
            return;
        for (int i = 0; i < stars.Count; i++)
            stars[i].transform.localScale = new Vector3(0, 0, 0);
        doubleReward = 0;
        rewardText.text = doubleReward + "$";
        stars[0].transform.DOScale(new Vector3(1, 1, 1), 0.5f).SetDelay(1.15f).SetEase(Ease.InOutQuad).OnComplete(() =>
        {
            if (PassengerManager.Instance.scanRightNum >= 2)
                stars[1].transform.DOScale(new Vector3(1, 1, 1), 0.5f).SetEase(Ease.InOutQuad).OnComplete(() =>
                {
                    if (PassengerManager.Instance.scanRightNum >= 3)
                        stars[2].transform.DOScale(new Vector3(1, 1, 1), 0.5f).SetEase(Ease.InOutQuad);
                });
            // 结算奖励
            doubleReward = Random.Range(10, 50) * PassengerManager.Instance.scanRightNum;
            if (Random.Range(0, 2) == 0)
                doubleReward *= PlayerPrefs.GetInt("Date", 1);
            DOTween.To(() => currentReward, x => currentReward = x, doubleReward, 0.75f).OnUpdate(() =>
            {
                rewardText.text = currentReward + "$";
            });
        });

    }

    // 领取奖励
    public void OnColletcBtnClick()
    {
        Debug.Log("领取每日奖励");
        AudioManager.Instance.PlayButtonSound();
        PlayerPrefs.SetInt("Date", PlayerPrefs.GetInt("Date", 1) + 1);
        PlayerPrefs.SetInt("Money", PlayerPrefs.GetInt("Money", 100) + currentReward);

        modelAnim.SetBool("Walk", false);
        modelAnim.SetTrigger("Idle");
        modelAnim.transform.DOLocalMove(new Vector3(0, 0, 0f), 0.1f).SetEase(Ease.Linear);
        nextUI = gameMenu;
        if (PassengerManager.Instance.currentPassenger.Count > 0)
            PassengerManager.Instance.ClearAllPassenger();
        LoadMaskAnim(0f);
    }

    // 双倍奖励
    public void OnDoubleRewardClick()
    {
        AudioManager.Instance.PlayButtonSound();
        Debug.Log("奖励翻倍");
        doubleReward = currentReward * 2;
        DOTween.To(() => currentReward, x => currentReward = x, doubleReward, 0.75f).OnUpdate(() =>
        {
            rewardText.text = currentReward + "$";
        });
        rewardText.text = currentReward + "$";
        doubleBtn.SetActive(false);
    }


    // 转场动画
    private void LoadMaskAnim(float dely)
    {
        loadMask.gameObject.SetActive(true);
        loadMask.DOFade(1f, 0.75f).SetDelay(dely).OnComplete(() =>
        {
            // 切换界面
            if (nextUI != null)
            {
                currentUI.SetActive(false);
                nextUI.SetActive(true);
                currentUI = nextUI;
                nextUI = null;
                // 界面状态
                if (currentUI == gameMenu)
                {
                    AudioManager.Instance.BGMController(true);
                }
            }
            // 切换相机
            if (currentUI == passengerMenu)
                cameraManager.ChangeCameraPos(cameraManager.cameraPos.passengerPos_0, 0.1f);
            else if (currentUI == gameMenu)
                cameraManager.ChangeCameraPos(cameraManager.cameraPos.originalPos, 0.1f);
            // 切换角色性别
            if (changeGender)
            {
                changeGender = false;
                modelAnim.GetComponent<GenderManager>().ShowCharacterModel();
            }
            // 更新数据
            if (currentUI == gameMenu)
                UpdateDate();

            loadMask.DOFade(0f, 0.5f).OnComplete(() =>
              {
                  loadMask.gameObject.SetActive(false);
                  if (currentUI == passengerMenu)
                      cameraManager.ChangeCameraPos(cameraManager.cameraPos.passengerPos_1, 0.75f);
              });
        });
    }
}
